package com.dao.impl;

import com.Bean.EmployeeTwo;
import com.dao.EmployeeTwoDao;
import com.util.JdbcUtil;
import com.util.PageUtil;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class EmployeeTwoDaoImpl implements EmployeeTwoDao {
    @Override
    public int insert(EmployeeTwo employee) throws SQLException {
        String sql = "INSERT INTO employee(eid,ename,epwd,estatus,ephone,eicon) VALUES(?,?,?,?,?,?)";

        return  JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{
                employee.getEid(),employee.getEname(),employee.getEpwd(),
                employee.getEstatus(),employee.getEphone(),employee.getEicon()
        });
    }

    @Override
    public List<EmployeeTwo> queryData(EmployeeTwo employee) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "SELECT eid,ename,estatus,epwd,ephone,eicon FROM employee";

        return JdbcUtil.queryData(sql, EmployeeTwo.class);
    }



    @Override
    public int update(EmployeeTwo employee) throws SQLException {
        String sql = "UPDATE `employee` SET ename=?,epwd=?,estatus=?,ephone=?,eicon=? WHERE eid=?";

        return  JdbcUtil.insertOrUpdateOrDelete(sql,new Object[]{
                employee.getEname(),employee.getEpwd(),employee.getEstatus(),
                employee.getEphone(),employee.getEicon(),employee.getEid()
        });
    }

    @Override
    public int deleteById(Object id) throws SQLException {
        String sql = "DELETE FROM employee WHERE eid=?";

        return JdbcUtil.insertOrUpdateOrDelete(sql, new Object[]{id});
    }

    @Override
    public List<EmployeeTwo> queryDataForText(EmployeeTwo employee) throws IllegalAccessException, SQLException, InstantiationException {
        String sql = "SELECT eid,ename,estatus FROM employee ";
        System.out.println(employee);
        if (!(employee == null)) {
            if (employee.getEstatus().equals("在职")) {
                sql += " WHERE estatus='在职' ";
                if (!(employee.getEid()==null)) {
                    sql += " AND (eid LIKE '%"+employee.getEid()+"%' OR ename LIKE '%"+employee.getEname()+"%')";
                }
            } else if (employee.getEstatus().equals("离职")) {
                sql += " WHERE estatus='离职' ";
                if (!(employee.getEid()==null)) {
                    sql += " AND (eid LIKE '%"+employee.getEid()+"%' OR ename LIKE '%"+employee.getEname()+"%')";
                }
            } else {
                if (!(employee.getEid()==null)) {
                    sql += " WHERE (eid LIKE '%"+employee.getEid()+"%' OR ename LIKE '%"+employee.getEname()+"%')";
                }
            }
        }
        return JdbcUtil.queryData(sql, EmployeeTwo.class);
    }

    @Override
    public PageUtil<EmployeeTwo> queryDataBypage(int currentPgae, int pageSize, EmployeeTwo employee) throws IllegalAccessException, SQLException, InstantiationException {

        //SELECT * from dd_books where 1=1
        //SELECT * from dd_books where 1=1 and book_name like ?
        //SELECT * from dd_books where 1=1 and book_name like ?  and type_name like ?
        //SELECT * from dd_books where 1=1   and type_name like ?

        String sql  = "SELECT eid,ename,estatus,epwd,ephone,eicon FROM employee where 1=1";
        ArrayList values = new ArrayList();
        if (employee != null){
            if (employee.getEstatus().equals("全部")){
                if (!employee.getEid().equals("")){
                    sql += " AND (eid LIKE ? OR ename LIKE ?) ";
                    values.add("%"+employee.getEid()+"%");
                    values.add("%"+employee.getEname()+"%");
                }
            }else {
                sql += " AND estatus=? ";
                values.add(employee.getEstatus());
                if (!employee.getEid().equals("")){
                    sql += " AND (eid LIKE ? OR ename LIKE ?) ";
                    values.add("%"+employee.getEid()+"%");
                    values.add("%"+employee.getEname()+"%");
                }
            }
        }
        return JdbcUtil.queryDataByPage(sql, EmployeeTwo.class, currentPgae,pageSize ,values.toArray());


    }
}
